import { defineStore } from 'pinia';
import { getRightData } from '@/api/earlyWarning';
import { onMounted } from 'vue';
import { TopList } from '@/constants/components/topComponents';
import { mapBackendData, type moduleInfo } from '@/utils/mapBackendData';
import { useCesiumPoints } from '@/hooks/useCesiumPoints';
import { videoMonitor } from '@/api/topComponents';
import { MapDeviceTypes } from '@/enums/MapDeviceTypesEnum';
import componentsTitle from '@/constants/components/componentsTitle';
import { useDialogStore } from '@/store/modules/useDialogStore';

export const useTopStore = defineStore('top', () => {
  const topList = ref<moduleInfo[]>(TopList);
  const { addPoint, clearAllPoints } = useCesiumPoints();
  const dataSource = reactive<Record<string, any[]>>({});
  const { openDialog } = useDialogStore();
  const currentList = ref<any[]>([]);
  const { flyTo } = useDialogStore();
  const TOP_LIST_MAPING = {
    [componentsTitle.MONITOR_STRENGTH]: { countKey: 'monitorPoints', dataKey: 'equipmentOverhauls' },
    [componentsTitle.REAL_TIME_MONITORING]: { countKey: 'xueLiangVideoNums' },
    [componentsTitle.THIRD_PARTY_VANDALISM_ALARMS]: { countKey: 'nengYuanVideoNums' },
    [componentsTitle.ALERT_TOTAL]: { countKey: 'warningTotal', dataKey: 'waringInfos' },
    [componentsTitle.ALARM_TOTAL]: { countKey: 'alarmTotal', dataKey: 'alarmInfos' }
  } as const; // as const确保类型推断为字面量类型
  const setModuleTitle = (name, item) => {
    if (name === componentsTitle.ALERT_TOTAL) {
      return componentsTitle.ALERT_OVERVIEW;
    } else if (name === componentsTitle.ALARM_TOTAL) {
      return componentsTitle.ALARM_TYPE;
    } else if (name === componentsTitle.MONITOR_STRENGTH) {
      return MapDeviceTypes(item.monitorType);
    }
  };
  const getDialogType = (name: string) => {
    switch (name) {
      case componentsTitle.MONITOR_STRENGTH:
        openDialog('monitoringForceModule');
        break;
      case componentsTitle.ALERT_TOTAL:
        openDialog('topAlertOverviewModule');
        break;
      case componentsTitle.ALARM_TOTAL:
        openDialog('topAlarmTypeModule');
        break;
      case componentsTitle.REAL_TIME_MONITORING:
        openDialog('realTimeMonitoringModule');
        break;
      case componentsTitle.THIRD_PARTY_VANDALISM_ALARMS:
        openDialog('thirdPartyVandalismAlarmsModule');
    }
  };
  const addEntitiesHandle = async (item) => {
    try {
      flyTo({ longitude: 117.140035075, latitude: 36.798747059 }, 400000);
      item.clickStatus = !item.clickStatus;
      if (item.clickStatus) {
        // 处理点击开启状态
        if (!item.list) {
          // 需要调用接口获取数据的情况
          const requests = {
            [componentsTitle.REAL_TIME_MONITORING]: () => videoMonitor({ monitorType: '05' }),
            [componentsTitle.THIRD_PARTY_VANDALISM_ALARMS]: () => videoMonitor({ type: '1,3', monitorType: '01,03,04' })
          };

          if (requests[item.name]) {
            const { rows } = await requests[item.name]();
            item.list = rows;
          }
        }
        currentList.value = item.list;
        getDialogType(item.name);
        // 添加点到数据源
        dataSource[item.name] = item.list.map((point) => addPoint(point, item.name, setModuleTitle(item.name, point)));
      } else {
        // 处理点击关闭状态
        if (dataSource[item.name]) {
          clearAllPoints(dataSource[item.name]);
          delete dataSource[item.name];
        }
      }
    } catch (error) {
      console.error('Error in addEntitiesHandle:', error);
      // 可以根据需要恢复clickStatus或进行其他错误处理
      item.clickStatus = !item.clickStatus; // 恢复原始状态
    }
  };
  const initClearPoint = () => {
    for (const key in dataSource) {
      clearAllPoints(dataSource[key]);
      delete dataSource[key];
    }
    topList.value.forEach((item) => {
      item.clickStatus = false;
    });
  };
  //单独绘制报警点位 点击点位时清除所有的报警总数点位 然后打单独点击的点位
  const addAlarmPoint = (item) => {
    initClearPoint();
    flyTo(item);
    dataSource[item.name] = [addPoint(item, componentsTitle.ALARM_TOTAL, componentsTitle.ALARM_TYPE)];
  };
  onMounted(async () => {
    const { data } = await getRightData({ type: 'topData' });
    topList.value = mapBackendData(TopList, data.topData, TOP_LIST_MAPING);
  });

  return {
    topList,
    currentList,
    addAlarmPoint,
    addEntitiesHandle,
    initClearPoint
  };
});
